//
//  LJFSortView.m
//  SevenZhou
//
//  Created by qianfeng on 15/9/24.
//  Copyright (c) 2015年 ljf. All rights reserved.
//

#import "LJFSortView.h"

@implementation LJFSortView


#pragma mark - 初始化

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self loadFirstButtonWithFrame:frame];
    }
    return self;
}

#pragma mark - 加载按钮

- (void)loadFirstButtonWithFrame:(CGRect)frame
{
    _sectionBtn = [[UIButton alloc] initWithFrame:self.bounds];
    
    [_sectionBtn setBackgroundImage:[UIImage imageNamed:@"footer_active_bg.png"] forState:UIControlStateNormal];
    
    [_sectionBtn addTarget:self action:@selector(sectionBtnTouch:) forControlEvents:UIControlEventTouchUpInside];
    
    NSString *sectionBtnTitle = @"全部目的地";
    
    [_sectionBtn  setTitle:sectionBtnTitle forState:UIControlStateNormal];
    [_sectionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _sectionBtn.titleLabel.font = [UIFont boldSystemFontOfSize:17.0f];
    [self addSubview:_sectionBtn];
    
    UIImageView *sectionBtnIv = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width - 16), (40-12)/2, 12, 12)];
    [sectionBtnIv setImage:[UIImage imageNamed:@"down_dark.png"]];
    [sectionBtnIv setContentMode:UIViewContentModeScaleToFill];
    
    [self addSubview: sectionBtnIv];
}


#pragma mark - 按钮触发事件

- (void)sectionBtnTouch:(UIButton *)button
{
    if (_isShow)
    {
        
        [self hiddenHiddenView];
        
    }
    else
    {
        
        [self showhiddenView];
        
        [_mydelegate SortViewremove];
    }
}

#pragma mark - 显示Collection

- (void)showhiddenView
{
    _isShow = YES;
    
    _hiddenview = [[UIView alloc] initWithFrame:CGRectMake(0,106, _supview.frame.size.width, _supview.frame.size.height - 106)];
    _hiddenview.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.5];
    [self.supview addSubview:_hiddenview];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [_hiddenview addGestureRecognizer:tap];
    
    [self loadheadView];
    
    [self creatCollectionViewTosupView:_supview];
}

- (void)loadheadView
{
    _headView = [[NSBundle mainBundle] loadNibNamed:@"LJFsortHeadView" owner:self options:0][0];
    _headView.frame = CGRectMake(0, 106, _supview.frame.size.width, 40);
    _headView.mydelegate = self;
    
    [_supview addSubview:_headView];
}

- (void)creatCollectionViewTosupView:(UIView *)view
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    layout.itemSize = CGSizeMake(_supview.frame.size.width, _supview.frame.size.height - 150 - 146);
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 146, _supview.frame.size.width, _supview.frame.size.height - 150 - 146)collectionViewLayout:layout];
    
    _collectionView.dataSource =self;
    _collectionView.delegate =self;
    _collectionView.pagingEnabled =YES;
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    CGRect rect = self.collectionView.frame;
    rect.origin.x = 0;
    rect.size.width = view.frame.size.width;
    rect.size.height = 0;
    self.collectionView.frame = rect;
    
    rect .size.height = view.frame.size.height - 150 - 146;
    [UIView animateWithDuration:0.3 animations:^
     {
         self.collectionView.alpha = 0.2;
         self.hiddenview.alpha = 0.2;
         
         self.collectionView.alpha = 1.0;
         self.hiddenview.alpha = 1.0;
         self.collectionView.frame =  rect;
         
     }];
    [view addSubview:_collectionView];
    
    [_collectionView registerNib:[UINib nibWithNibName:@"LJFSortCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"LJFSortCollectionCell"];
    
    [self.collectionView reloadData];
    
}

#pragma mark - 隐藏Collection

- (void)hiddenHiddenView
{
    _isShow = NO;
    
    CGRect rect = self.collectionView.frame;
    rect.size.height = 0;
    [UIView animateWithDuration:0.3 animations:^
     {
         self.hiddenview.alpha = 1.0f;
         self.collectionView.alpha = 1.0f;
         
         self.hiddenview.alpha = 0.2f;
         self.collectionView.alpha = 0.2;
         
         self.collectionView.frame = rect;
         
     }completion:^(BOOL finished)
     {
         [self.tableView removeFromSuperview];
         [self.hiddenview removeFromSuperview];
         [self.headView removeFromSuperview];
     }];
}

- (void)tap:(UITapGestureRecognizer *)tap
{
    [self hiddenHiddenView];
}

#pragma mark - 重写set方法

-(void)setSevenArr:(NSArray *)sevenArr
{
    _sevenArr = sevenArr;
    
    [_collectionView reloadData];
}

-(void)setIsRemove:(BOOL)isRemove
{
    _isRemove = YES;
    
    [self hiddenHiddenView];
}

-(void)setButtonName:(NSString *)buttonName
{
    _buttonName = buttonName;
    
    [_sectionBtn setTitle:buttonName forState:UIControlStateNormal];
}

#pragma mark -sortHeadViewDelegate

- (void)sortHeadView:(LJFsortHeadView *)view andoffset:(NSInteger)index
{
    [_collectionView setContentOffset:CGPointMake(_collectionView.frame.size.width*index, 0) animated:YES];
}

#pragma mark - SortCollectionCellDelegate

-(void)SortCollectionCell:(LJFSortCollectionCell *)cell didSelectWithindex1:(NSInteger)index1 andIndex2:(NSInteger)index2 andoffset:(NSInteger)offset andTitle1:(NSString *)title1 andTitle2:(NSString *)title2
{
    
    _index1 = index1;
    _index2 = index2;
    
    _headView.name1 = title1;
    _headView.name2 = title2;
    
    [_headView.contentButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_headView.coutryButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    
    [_collectionView setContentOffset:CGPointMake(_collectionView.frame.size.width*offset, 0) animated:YES];
}

- (void)removehiddenView:(NSString *)goodsid and:(NSString *)name
{
    [_mydelegate getSelectedGoodsId:goodsid];
    
    [_sectionBtn setTitle:name forState:UIControlStateNormal];
    [self hiddenHiddenView];
}

#pragma mark -UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    LJFSortCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LJFSortCollectionCell" forIndexPath:indexPath];
    cell.mydelegate = self;
    cell.index1 = _index1;
    cell.index2 = _index2;
    cell.dataArr = _sevenArr;
    cell.collectionRow = indexPath.row;
    
    return cell;
}


@end
